﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class HopDongDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("selehd * from HopDong");
        }

        public HopDong GetById(String maHD)
        {
            HopDong hd = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("selehd * from HopDong where maHD = '" + maHD + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    hd = new HopDong();
                    hd.MaHD = dataReader.GetString(0);
                    hd.TenHD = dataReader.GetString(1);
                    hd.NgayKy = dataReader.GetDateTime(2);
                    hd.NgayHetHan = dataReader.GetDateTime(3);
                    hd.MaCT = dataReader.GetString(4);
                    hd.MaNSX = dataReader.GetString(5);
                    hd.MaNV = dataReader.GetString(6);
                    return hd;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return hd;
        }

        public bool Insert(HopDong hd)
        {
            String sql = "INSERT INTO [QLVT].[dbo].[HopDong]([MaHD],[TenHD],[NgayKy],[NgayHetHan],[MaCT],[MaNSX],[MaNV])"
               + "VALUES('" + hd.MaHD + "','" + hd.TenHD + "'," + hd.NgayKy + ",'" + hd.NgayHetHan + "','" + hd.MaCT + "','" + hd.MaNSX + "','"  + hd.MaNV + "')";
            return Update(sql);
        }

        public bool DeleteById(String mahd)
        {
            return Update("delete from HopDong where MaHD = '" + mahd + "'");
        }

        public bool UpdateById(HopDong hd)
        {
            String sql = "update HopDong "
                 + " set TenHD = '" + hd.TenHD + "'"
                 + " set NgayKy = '" + hd.NgayKy + "'"
                 + " set NgayHetHan = '" + hd.NgayHetHan + "'"
                 + " set MaCT = '" + hd.MaCT + "'"
                 + " set MaNSX = '" + hd.MaNSX + "'"
                 + " set MaNV = '" + hd.MaNV + "'"
                 + " where MAHD =  '" + hd.MaHD + "'";
            return Update(sql);
        }
    }
}
